const app = getApp()
import Common from '../../common/common.js';
let common = new Common;
Page({
  data: {
    showtext: true,//防止输入内容重影
    active: 9,
    list: [],
    pageNumber: 1,
    size: 10,
    isEnd: false, //是否到底
    search_key: '',
    orgSeq: '',//店铺id 
    userType: '',
    userId: '',
    lineName: '全部待付',
    linenum: 0,
    payMethod: 9,
    orderSources: 1,//1,线上商城 7定制酒
    hidden: true,//默认隐藏弹框
    orderTypeList: [
      { id: 0, name: '待付款', selected: false }, { id: 1, name: '待发货', selected: false }, { id: 2, name: '待收货', selected: false }, { id: 3, name: '已完成', selected: false }
      // ,{id:5,name:'已退款',selected:false}
    ],//订单状态列表
    arr: [], //标签数组：用来存储选中的值
    orderType: '',
    orderLxList: [
      { id: 1, name: '含赠品订单' }, { id: 2, name: '宴席订单' }
      // { id: 1, name: '含赠品订单' }, { id: 0, name: '线下付款订单' }
    ],//订单类型
    orderTypeId: null,//选中订单类型\
    dxOrderStatus: null,//拼接字符串
    orderStatus: null,//单个传参
    isGiftOrder: null,//含赠品订单参数
    showPrewImage: false,
    isFeastOrder:null,//宴席订单
  },
  // 跳转问题详情
  detile:function(){
    wx.navigateTo({
      url: '/pages/myCenter/problemDetil/problemDetil?code='+3+'&type='+1,
    })
  },
  //跳转到我的配送单 
  toMyorderList: function (e) {
    wx.navigateTo({
      url: '/pages/myOrderMgt/index',
    })
  },
  //查看
  previewImage(e) {
    let imageurl = e.currentTarget.dataset.url;
    wx.previewImage({
      urls: [imageurl],
      current: imageurl,
      success: res => {
        console.log('预览')
      }
    })
  },
  hideShows() {
    this.setData({
      imageurl: '',
      showPrewImage: false
    })
  },
  // 商家确认收货（定制酒）
  confirmGoods(e) {
    let that = this
    wx.showModal({
      title: '提示',
      content: '是否确认收货',
      success(res) {
        if (res.confirm) {
          let data = {
          }
          common.requestAjax('/api/trade-service/v1/tradeNotification/initCustomizeStatus?uuid=' + e.currentTarget.dataset.uuid + "&customizeStatus=" + e.currentTarget.dataset.customizestatus, 'POST', data).then(res => {
            if (res.data.code == 1) {
              //修改数组中对象的值
              let index = e.currentTarget.dataset.indexs
              let customizeStatus = `list[${index}].customizeStatus`
              that.setData({
                [customizeStatus]: 7,
              })
              wx.showToast({
                title: '您已确认收货',
                icon: 'success'
              })
            } else {
              common.showToast(res.data.message)
            }
          }).catch(err => {
            console.log(err);
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  // 空白事件
  blank() {

  },
  // 点击订单类型
  orderType: function (e) {
    let id = e.currentTarget.dataset.id;
    if (this.data.orderTypeId == id) {
      this.setData({
        orderTypeId: null,
        isGiftOrder: null,
        isFeastOrder:null,
        payMethod: 9,
      })
    } else {
      this.setData({
        orderTypeId: id
      })
      if (this.data.orderTypeId == 0) {
        this.setData({
          payMethod: 0,
          isGiftOrder: null
        })
      }else if(this.data.orderTypeId == 2){
        this.setData({
          isFeastOrder:1
        })
      }else {
        this.setData({
          isGiftOrder: 1,
          payMethod: 9,
        })
      }
    }
    console.log(this.data.orderTypeId,'选中的id')
    console.log(id,'点击的id')
  },
  // 点击订单状态
  product: function (e) {
    // 多选
    var that = this
    let index = e.currentTarget.dataset.index;//下标
    let value = e.currentTarget.dataset.id;//id
    let items = that.data.orderTypeList;
    let arr = that.data.arr;
    let val = items[index].selected; //点击前的值
    items[index].selected = !val;
    if (items[index].selected == true) {  //true 数组拼接  
      arr.push(value);
      arr = [...new Set(arr)];
    } else {
      Array.prototype.remove = function (value) {
        var len = this.length;
        for (var i = 0, n = 0; i < len; i++) {//把出了要删除的元素赋值给新数组
          if (this[i] != value) {
            this[n++] = this[i];
          } else {
            console.log(i);//测试所用
          }
        }
        this.length = n;
      };
      this.data.orderType.remove(value);
    }
    // console.log(arr)
    // active
    let active = arr;
    if (arr.length > 1) {
      active = active.toString();
      this.setData({
        active: 9
      })
    } else {
      active = items[index].id;
      this.setData({
        active: active
      })
    }
    console.log(arr.length, '数组长度')
    that.setData({
      orderTypeList: items,
      arr: arr,
      orderType: arr,
      dxOrderStatus: active,
    })
    console.log(this.data.orderTypeList)
    console.log(this.data.arr)
    console.log(this.data.orderType)
    console.log(this.data.active)
  },
  // 确定取消
  hideMask: function (e) {
    if (e.currentTarget.dataset.no == 0) {
      console.log(22)
      let orderTypeList = this.data.orderTypeList;
      orderTypeList.forEach(item => {
        item.selected = false
      })
      this.setData({
        orderTypeList: orderTypeList,
        orderType: [],
        orderTypeId: null,
      })
    } else {
      // 判断订单状态数组是否为空  防止报错
      if (this.data.dxOrderStatus) {
        if (this.data.dxOrderStatus.length > 1) {
          // 订单状态数组 长度大于1  循环转换字符串
          let dxOrderStatus = [];
          for (var i = 0; i < this.data.orderTypeList.length; i++) {
            if (this.data.orderTypeList[i].selected == true) {
              dxOrderStatus.push(this.data.orderTypeList[i].id)
            }
          }
          this.setData({
            dxOrderStatus: dxOrderStatus.toString(),
          });
        } else {
          // 订单状态数组 长度小于1  穿单个字符串
          let dxOrderStatus = '';
          for (var i = 0; i < this.data.orderTypeList.length; i++) {
            if (this.data.orderTypeList[i].selected == true) {
              dxOrderStatus = this.data.orderTypeList[i].id
            }
          }
          this.setData({
            dxOrderStatus: dxOrderStatus.toString()
          });
        }
      }
      // if(this.data.dxOrderStatus.length>1){
      //   let dxOrderStatus=[];
      //   for(var i = 0;i<this.data.orderTypeList.length;i++){
      //     if(this.data.orderTypeList[i].selected==true){
      //       dxOrderStatus.push(this.data.orderTypeList[i].id)
      //     }
      //   }
      //   this.setData({
      //     dxOrderStatus:dxOrderStatus.toString(),
      //     hidden: true,
      //   }); 
      // }else{
      //   let dxOrderStatus='';
      //   for(var i = 0;i<this.data.orderTypeList.length;i++){
      //     if(this.data.orderTypeList[i].selected==true){
      //       dxOrderStatus=this.data.orderTypeList[i].id
      //     }
      //   }
      //   this.setData({
      //     dxOrderStatus:dxOrderStatus.toString(),
      //     hidden: true,
      //   }); 
      // }
      this.setData({
        hidden: true,
      });
      this.getProductList(1, 1);
    }
  },
  // 点击筛选出现弹框
  showMask: function (e) {
    for (var i = 0; i < this.data.orderTypeList.length; i++) {
      if (this.data.active == this.data.orderTypeList[i].id) {
        console.log(this.data.active)
        this.data.orderTypeList[this.data.active].selected = true;
      }
    }
    this.setData({
      hidden: false,
      orderTypeList: this.data.orderTypeList
    })
  },
  hideselect: function (e) {
    this.setData({
      lineNum: 0
    })
  },
  // 切换普通和定制订单
  changeType(e) {
    this.setData({
      orderSources: e.currentTarget.dataset.type
    })
    this.getProductList(1, 1);
  },
  // 点击显示选择
  showselect: function (e) {
    let line = e.currentTarget.dataset.linenum;
    if (line == 0) {
      this.setData({
        lineNum: 1
      })
    } else {
      this.setData({
        lineNum: 0
      })
    }
  },
  // 线下付款筛选 0线下付款  1 全部代付
  setPayLine: function (e) {
    let line = e.currentTarget.dataset.line;
    if (line == 9) {
      this.setData({
        lineName: '全部待付'
      })
    } else {
      this.setData({
        lineName: '线下付款'
      })
    }
    this.setData({
      payMethod: line,
      lineNum: 0
    })
    this.getProductList(1, 1)
  },
  //回到顶部
  goTop: function (e) {  // 一键回到顶部
    if (wx.pageScrollTo) {
      wx.pageScrollTo({
        scrollTop: 0
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
    }
  },
  order: function (e) {
    let orderTypeList = this.data.orderTypeList.map(item => {
      item.selected = false;
      return item
    })
    this.setData({
      active: e.currentTarget.dataset.active,
      dxOrderStatus: e.currentTarget.dataset.active,
      lineNum: 0,
      orderTypeList: orderTypeList
    });

    if (this.data.active != 0) {
      this.setData({
        payMethod: 9,
        lineName: '全部待付'
      })
    }
    this.goTop()//回到顶部
    this.getProductList(1, 1)
  },
  info: function (e) {
    this.setData({
      lineNum: 0
    })
    let { ordersource, id } = e.currentTarget.dataset;
    let routeUrl = '/pages/orderMgt/orderInfo/orderInfo' + '?type=' + this.data.active + '&id=' + id;

    if (ordersource == 8) {
      routeUrl = '/pages/orderMgt/orderInfo/activeOrder915/orderInfo' + '?type=' + this.data.active + '&id=' + id;
    }

    wx.navigateTo({
      url: routeUrl,
    })
  },
  onPullDownRefresh: function () {
    //清空搜索框
    this.setData({
      value: '',
      pageNumber: 1,
      isEnd: false
    })
    this.getProductList(1, 1)
    setTimeout(() => {
      wx.stopPullDownRefresh();
    }, 1200)
  },
  onReachBottom: function () {
    if (!this.data.isEnd) {
      let pageNumber = this.data.pageNumber;
      pageNumber += 1;
      this.getProductList(pageNumber, 2)
      wx.showLoading({
        title: '正在加载中...',
      })
      this.setData({
        pageNumber
      })
    }
  },
  // 获取焦点
  bindFocusKey: function (e) {
    this.setData({
      lineNum: 0
    })
  },
  searchList: function (e) {
    // 防止输入内容重影
    if (e.detail.value == '') {
      this.setData({
        showtext: true
      })
    } else {
      this.setData({
        showtext: false
      })
    }
    this.setData({
      search_key: e.detail.value
    })
    this.getProductList(1, 1)
  },
  getProductList(page, type) {
    // wx.showLoading({
    //   title: '加载中',
    // }) 
    let returnStatus = '';
    let dxOrderStatus = this.data.active;
    console.log(this.data.active, '=====')
    if (dxOrderStatus == '4') {
      returnStatus = 1;
      this.setData({
        dxOrderStatus: 9
      })
    } else {
      returnStatus = 9;
    }
    let data = {
      condition: {
        orgSeq: this.data.orgSeq,
        orderStatus: this.data.orderStatus,//0待付款 1待发货 2待收货 3完成 4取消 9全部
        keyWords: this.data.search_key,
        userId: '',
        returnStatus: returnStatus,
        payMethod: this.data.payMethod,
        orderSources: this.data.orderSources,
        dxOrderStatus: this.data.dxOrderStatus,
        isGiftOrder: this.data.isGiftOrder,//赠品订单参数
        isFeastOrder:this.data.isFeastOrder//宴席订单
      },
      current: page,
      size: 10,
    }
    if (this.data.userType == 6) {
      data.condition.userId = this.data.userId;
    }
    common.requestAjax('/api/trade-service/v1/myorder/queryStoreOrders', 'POST', data).then(res => {
      if (res.data.code == 1) {
        wx.hideLoading()
        let list = this.data.list || [];
        let isEnd = this.data.isEnd;
        if (type == 1) {
          //下拉刷新
          list = res.data.records;
          isEnd = false;
        } else if (type == 2) {
          //上拉加载
          list = list.concat(res.data.records);
          if (res.data.records.length == 0) {
            //最后一行没数据 到底了
            isEnd = true;
          }
          // setTimeout(() => {
          //   wx.hideLoading()
          // }, 500)
        }
        this.setData({
          list: list,
          isEnd,
        })
      } else {
        common.showToast(res.data.message)
      }
    }).catch(err => {
      console.log(err);
    })
  },
  onLoad: function (options) {
    console.log(options)
    let orgSeq = app.globalData.orgSeq;
    this.setData({
      active: options.type,
      dxOrderStatus: options.type,
      orgSeq: orgSeq,
      userType: app.globalData.shopInfo.userType,
      userId: app.globalData.userId,

    })
  },
  onShow: function () {
    this.getProductList(1, 1)
  }
})